#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""code_info
@Time    : 2020 2020/7/8 9:43
@Author  : ao
@File    : bili_user_info.py
"""
import requests
import re
import time
import random
from . import createAndConnectDB as db

db.init()
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 " +
                 "Safari/537.36 Core/1.70.3766.400 QQBrowser/10.6.4163.400 ",

}

for i in range(1, 100):
    r = requests.get("https://api.bilibili.com/x/space/acc/info?mid=" + str(i) + "&jsonp=jsonp", headers=headers)
    if r.status_code == 200:
        re_mid = re.compile(r'"mid":([\s\S]*?),')
        re_name = re.compile(r'"name":"([\s\S]*?)",')
        re_sign = re.compile(r'"sign":"([\s\S]*?)",')
        re_level = re.compile(r'"level":([\s\S]*?),')
        mid = re_mid.findall(r.text)
        name = re_name.findall(r.text)
        sign = re_sign.findall(r.text)
        level = re_level.findall(r.text)
        if len(mid) > 0 and len(name) > 0 and len(level) > 0 and len(sign) > 0:
            db.insert(mid[0], name[0], sign[0], level[0])
            # print (mid[0], name[0], sign[0], level[0])
        time.sleep(1 + random.random())